﻿using System.Text.RegularExpressions;
using System;
using System.Linq;
using System.Globalization;

namespace MockCommon
{
    public sealed class Utility
    {  
        private static readonly char[] Replace = new char[7] { 'a', 'd', 'e', 'i', 'o', 'u', 'y' };
        private static readonly string[] Partern = new string[7] { "(á|à|ả|ã|ạ|ă|ắ|ằ|ẳ|ẵ|ặ|â|ấ|ầ|ẩ|ẫ|ậ)", "đ", "(é|è|ẻ|ẽ|ẹ|ê|ế|ề|ể|ễ|ệ)", "(í|ì|ỉ|ĩ|ị)", "(ó|ò|ỏ|õ|ọ|ô|ố|ồ|ổ|ỗ|ộ|ơ|ớ|ờ|ở|ỡ|ợ)", "(ú|ù|ủ|ũ|ụ|ư|ứ|ừ|ử|ữ|ự)", "(ý|ỳ|ỷ|ỹ|ỵ)" };

        /// <summary>
        /// Prevents a default instance of the <see cref="Utility"/> class from being created.
        /// </summary>
        private Utility()
        {
        }

        /// <summary>
        /// Removes the mark.
        /// </summary>
        /// <param name="accented">The accented.</param>
        /// <returns></returns>
        public static string RemoveMark(string accented)
        {
            if (accented != null)
            {
                for (int i = 0; i < Partern.Length; i++)
                {
                    MatchCollection matchs = Regex.Matches(accented, Partern[i], RegexOptions.IgnoreCase);
                    foreach (Match m in matchs)
                    {
                        accented = accented.Replace(m.Value[0], Replace[i]);
                    }
                }
            }
            return accented;
        }
  
        /// <summary>
        /// Stands the string.
        /// </summary>
        /// <param name="nameUser">The name user.</param>
        /// <returns></returns>
        public static string StandString(string nameUser)
        {
            nameUser = nameUser.Trim();
            nameUser = Regex.Replace(nameUser, @"\s+", " ");
            nameUser = nameUser.ToLower(CultureInfo.CurrentCulture);
            string fullNameStanded = "";
            if (nameUser.IndexOf(' ') != -1)
            {
                string[] name = nameUser.Split(' ');
                for (int i = 0; i < name.Length; i++)
                {
                    name[i] = name[i].Substring(0, 1).ToUpper(CultureInfo.CurrentCulture) + name[i].Substring(1, name[i].Length - 1);
                    fullNameStanded += name[i] + " ";
                }
            }
            else
            {
                fullNameStanded = nameUser.Substring(0, 1).ToUpper(CultureInfo.CurrentCulture) + nameUser.Substring(1, nameUser.Length - 1);
            }
            return fullNameStanded;
        }
    }
}
